package BNS.graphs;

import java.io.File;

import javax.swing.JFrame;

import BNS.plumbing.Library;


/** Derived from GraphPackBuilder.
	Used to create layouts for graphs; presently just prints the coords and stops;
	it would be nice to be able to store and reread layouts to improve on them.
	*/
public class GraphLayoutBuilder {
	
	public GraphLayoutBuilder(String fname) {
//		Library.init();
		makeAndSave(fname,1);
		}
	
	private void makeAndSave(String fname, int type) {
		JFrame frame= new JFrame("GraphPack Creation");
		frame.setSize(500,500);
		Creator crPanel= new Creator();
		frame.setContentPane(crPanel);
		frame.validate();
		frame.setVisible(true);	// must come BEFORE init, so that getSize() works
		
		int extras= 2;
		Graph gr= null;
		switch(type) {
		case 1: gr= MatrixGraph.create(new File(fname)); break;
		case 2: gr= RingChordGraph.create(frame); break;
		case 3: gr= PrefAttachGraph.create(frame); break;
		case 4: gr= Library.getPack(frame).graph; break;
			}
		GraphPack gp= null;
		if (gr!=null) {
			System.err.println("will lay out graph");
			gp= new GraphPack(gr,"creation model: "+type);
			buildLayouts(gp,crPanel,extras); 
			gp.graph.printLayout(System.out);
			System.err.println("printed layout");
			}
		else System.err.println("FAILED to construct graphpack ");
		}
		
	private void buildLayouts(GraphPack gp, Creator crPanel, int extras) {
		try {
			crPanel.init(gp.graph);
			System.err.println("GraphPack inited crPanel");
			synchronized(crPanel) { crPanel.wait();	}	// Creator's "next" button will kick this
			}
		catch(InterruptedException ie) {}
		}
	
	/** The main driver; invoke this from the command line to start the process. 
		Supply it with 1 argument: the name of the matrix file
		*/
	public static void main(String[] args) { 
		String fname= args[0];
		new GraphLayoutBuilder(fname); 
		System.exit(0);
		}
	
	} // EOClass GraphLayoutBuilder
